#include <MegabriteController.h>
#include <ColorMap.h>

MegabriteController* megabriteController;
ColorMap* colorMap;

void setup() {
  Serial.begin(9600);
  createColorMap();
  createMegabriteController();
  Serial.println("initialized");
}

void loop() {
  Serial.println("looping");
  for (int i = 0; i <= 100; i++) {
    sendColorValue(i);
  }
  for (int i = 100; i >= 0; i--) {
    sendColorValue(i);
  }
}

void sendColorValue(int i) {
    Color color0 = colorMap->getColorForValue(i);
    Color color1 = colorMap->getColorForValue(100 - i);    
    setMegabriteColor(0, color0);
    setMegabriteColor(1, color1);

    megabriteController->send();
    delay(50);
}

void setMegabriteColor(int megabriteIdx, Color color) {
    Megabrite* megabrite = megabriteController->getMegabriteAtIndex(megabriteIdx);
    megabrite->red = color.red;
    megabrite->green = color.green;
    megabrite->blue = color.blue;
}

void createMegabriteController() {
  megabriteController = newMegabriteController();
  megabriteController->init();
  megabriteController->setNumberOfMegabrites(2);
}

void createColorMap() {
  	colorMap = newColorMap();
	colorMap->addRedPoint(0, 1023);
	colorMap->addRedPoint(20, 0);
	colorMap->addRedPoint(40, 0);
	colorMap->addRedPoint(60, 1023);
	colorMap->addRedPoint(80, 0);
	colorMap->addRedPoint(100, 1023);

	colorMap->addGreenPoint(0, 0);
	colorMap->addGreenPoint(20, 0);
	colorMap->addGreenPoint(40, 1023);
	colorMap->addGreenPoint(60, 0);
	colorMap->addGreenPoint(80, 1023);
	colorMap->addGreenPoint(100, 1023);

	colorMap->addBluePoint(0, 0);
	colorMap->addBluePoint(20, 1023);
	colorMap->addBluePoint(40, 0);
	colorMap->addBluePoint(60, 1023);
	colorMap->addBluePoint(80, 1023);
	colorMap->addBluePoint(100, 1023);
}
